{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表查询慢\n",
    "for i in [['张三',98],['李四',99],['王五',85]]:\n",
    "    if i[0] == '王五':\n",
    "        print(f\"{i[0]} 的分数是 {i[1]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典查询快\n",
    "\n",
    "d = {'张三':98, '李四':99,'王五': 85}\n",
    "\n",
    "print(f\"王五 的分数是 {d['王五']}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#字典初始化\n",
    "\n",
    "d1 = {'name': 'jason', 'age': 20, 'gender': 'male'}\n",
    "\n",
    "d2 = dict({'name': 'jason', 'age': 20, 'gender': 'male'})\n",
    "\n",
    "d3 = dict([('name', 'jason'), ('age', 20), ('gender', 'male')])\n",
    "\n",
    "d4 = dict(name='jason', age=20, gender='male') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d1 == d2 == d3 ==d4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d = {'name': 'jason', 'age': 20}\n",
      "d = {'name': 'jason', 'age': 20, 'gender': '男', 'birthday': '1999-02-01'}\n",
      "d = {'name': 'jason', 'age': 20, 'gender': '女', 'birthday': '1999-02-01'}\n"
     ]
    }
   ],
   "source": [
    "#字典的增、改\n",
    "d = {'name': 'jason', 'age': 20}\n",
    "print(f\"{d = }\")\n",
    "d['gender'] = '男' # 增加元素对'gender': '男'\n",
    "d['birthday'] = '1999-02-01' # 增加元素对'birthday': '1999-02-01'\n",
    "print(f\"{d = }\")\n",
    "d['gender'] = '女' \n",
    "print(f\"{d = }\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d = {'name': 'jason', 'age': 20, 'gender': '女'}\n",
      "d = {'name': 'jason', 'gender': '女'}\n"
     ]
    }
   ],
   "source": [
    "#字典的删\n",
    "d.pop('birthday') # 删除键为'birthday'的元素对\n",
    "print(f\"{d = }\")\n",
    "del d['age']\n",
    "print(f\"{d = }\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#字典的查\n",
    "d['location']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.get('location','未找到')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#字典的 key 必须是可不可变的，数字、字符串、元组\n",
    "print(list(d))\n",
    "d[1] = \"abc\"\n",
    "d[\"1\"] = 123\n",
    "d[(1,2)]= 456\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d[(1,2,[1,2])] = 789\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "#集合\n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'set' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m-----------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-34-837164fcc466>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mbasket\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'set' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "basket[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'orange' in basket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'crabgrass' in basket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s = {1, 2, 3, 4}\n",
      "s = {1, 2, 3}\n",
      "s = {2, 3}\n"
     ]
    }
   ],
   "source": [
    "s.add(4) # 增加元素4到集合\n",
    "print(f\"{s = }\")\n",
    "s.remove(4) # 从集合中删除元素4\n",
    "print(f\"{s = }\")\n",
    "s.pop() #谨慎使用\n",
    "print(f\"{s = }\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'd', 'r'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = set('abracadabra')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'c', 'l', 'm', 'z'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = set('alacazam')\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b', 'd', 'r'}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a - b  # a 有 b 没有的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'd', 'l', 'm', 'r', 'z'}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a | b # a 有或 b 有的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'c'}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a & b # a b 都有的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b', 'd', 'l', 'm', 'r', 'z'}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ^ b # 要么在 a，要么在 b，但不能即在 a 又在 b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'d', 'r'}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {x for x in 'abracadabra' if x not in 'abc'} #集合也支持列表推导式\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'b': 1, 'a': 2, 'c': 10}\n",
    "d_sorted_by_key = sorted(d.items(), key=lambda x: x[0]) # 根据字典键的升序排序\n",
    "d_sorted_by_value = sorted(d.items(), key=lambda x: x[1]) # 根据字典值的升序排序\n",
    "print(f\"{d_sorted_by_key = }\")\n",
    "print(f\"{d_sorted_by_value = }\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Option A\n",
    "d = {'name': 'jason', 'age': 20, 'gender': 'male'}\n",
    "\n",
    "# Option B\n",
    "d = dict({'name': 'jason', 'age': 20, 'gender': 'male'})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
